﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ListAllInstaledDevices
{
    using System;
    using System.Management;
    // Enum Pnp Registered devices using WMI class Win32_PnPentity
    class App
    {
        public static void Main()
        {
            ManagementPath path = new ManagementPath();
            ManagementClass devs = null;
            path.Server = ".";
            path.NamespacePath = @"root\CIMV2";
            path.RelativePath = @"Win32_PnPentity";
            using (devs = new ManagementClass(new ManagementScope(path), path, new ObjectGetOptions(null, new TimeSpan(0, 0, 0, 2), true)))
            {
                ManagementObjectCollection moc = devs.GetInstances();
                foreach (ManagementObject mo in moc)
                {

                    PropertyDataCollection devsProperties = mo.Properties;
                    foreach (PropertyData devProperty in devsProperties)
                    {
                        if (devProperty.Type == CimType.DateTime)
                        {
                            if (devProperty.Value != null)
                                Console.WriteLine("Date {0}", ToDateTime(devProperty.Value.ToString()));
                        }
                        else
                            Console.WriteLine("Property = {0}\t Value = {1}",
                            devProperty.Name, devProperty.Value);
                    }

                    RelatedObjectQuery relatedQuery = new RelatedObjectQuery
                    ("associators of {Win32_PnPEntity.DeviceID='" + mo["DeviceID"] + "'}");
                    ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(new ManagementScope(path), relatedQuery);
                    foreach (ManagementObject mob in searcher.Get())
                    {

                        Console.WriteLine("--------------------------->>>>>>");
                        Console.WriteLine(mob["Description"]);

                    }
                    Console.WriteLine("----------------------");
                }

            }

        }
        // Converts a given datetime in DMTF format to System.DateTime object.
        static System.DateTime ToDateTime(string dmtfDate)
        {
            int year = System.DateTime.MinValue.Year;
            int month = System.DateTime.MinValue.Month;
            int day = System.DateTime.MinValue.Day;
            int hour = System.DateTime.MinValue.Hour;
            int minute = System.DateTime.MinValue.Minute;
            int second = System.DateTime.MinValue.Second;
            long ticks = 0;
            string dmtf = dmtfDate;
            System.DateTime datetime = System.DateTime.MinValue;
            string tempString = System.String.Empty;
            if ((dmtf == null))
            {
                throw new System.ArgumentOutOfRangeException();
            }
            if ((dmtf.Length == 0))
            {
                throw new System.ArgumentOutOfRangeException();
            }
            if ((dmtf.Length != 25))
            {
                throw new System.ArgumentOutOfRangeException();
            }
            try
            {
                tempString = dmtf.Substring(0, 4);
                if (("****" != tempString))
                {
                    year = System.Int32.Parse(tempString);
                }
                tempString = dmtf.Substring(4, 2);
                if (("**" != tempString))
                {
                    month = System.Int32.Parse(tempString);
                }
                tempString = dmtf.Substring(6, 2);
                if (("**" != tempString))
                {
                    day = System.Int32.Parse(tempString);
                }
                tempString = dmtf.Substring(8, 2);
                if (("**" != tempString))
                {
                    hour = System.Int32.Parse(tempString);
                }
                tempString = dmtf.Substring(10, 2);
                if (("**" != tempString))
                {
                    minute = System.Int32.Parse(tempString);
                }
                tempString = dmtf.Substring(12, 2);
                if (("**" != tempString))
                {
                    second = System.Int32.Parse(tempString);
                }
                tempString = dmtf.Substring(15, 6);
                if (("******" != tempString))
                {
                    ticks = (System.Int64.Parse(tempString)
                    * (System.TimeSpan.TicksPerMillisecond / 1000));
                }
                if (((((((((year < 0)
                || (month < 0))
                || (day < 0))
                || (hour < 0))
                || (minute < 0))
                || (minute < 0))
                || (second < 0))
                || (ticks < 0)))
                {
                    throw new System.ArgumentOutOfRangeException();
                }
            }
            catch (System.Exception e)
            {
                e = e;
                throw new System.ArgumentOutOfRangeException();
            }
            datetime = new System.DateTime(year, month, day, hour, minute, second, 0);
            datetime = datetime.AddTicks(ticks);
            System.TimeSpan tickOffset = System.TimeZone.CurrentTimeZone.GetUtcOffset(datetime);
            int UTCOffset = 0;
            long OffsetToBeAdjusted = 0;
            long OffsetMins = (tickOffset.Ticks / System.TimeSpan.TicksPerMinute);
            tempString = dmtf.Substring(22, 3);
            if ((tempString != "******"))
            {
                tempString = dmtf.Substring(21, 4);
                try
                {
                    UTCOffset = System.Int32.Parse(tempString);
                }
                catch (System.Exception e)
                {
                    e = e;
                    throw new System.ArgumentOutOfRangeException();
                }
                OffsetToBeAdjusted = (OffsetMins - UTCOffset);
                datetime = datetime.AddMinutes(OffsetToBeAdjusted);
            }
            return datetime;
        }
    }
}
